package com.ssm.order.exception;

import com.alibaba.cloud.sentinel.rest.SentinelClientHttpResponse;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.ssm.dto.CommonResult;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;

public class RestTemplateExceptionHandler {

    //限流熔断业务逻辑
    public static SentinelClientHttpResponse blockHandler(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
        String message = JSON.toJSONString(CommonResult.error(-100,"系统错误 (限流熔断业务逻辑)"));
        return new SentinelClientHttpResponse(message);
    }

    //异常降级业务逻辑
    public static SentinelClientHttpResponse fallback(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
        String message = JSON.toJSONString(CommonResult.error(-100,"系统错误 (异常降级业务逻辑)"));
        return new SentinelClientHttpResponse(message);
    }
}
